using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers;

/// <summary>
/// 标签控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class TagController : ControllerBase
{
    private readonly ITagService _tagService;

    public TagController(ITagService tagService)
    {
        _tagService = tagService;
    }

    /// <summary>
    /// 获取所有标签
    /// </summary>
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Tag>>> GetAll()
    {
        var tags = await _tagService.GetAllAsync();
        return Ok(tags);
    }

    /// <summary>
    /// 根据ID获取标签
    /// </summary>
    [HttpGet("{id}")]
    public async Task<ActionResult<Tag>> GetById(Guid id)
    {
        var tag = await _tagService.GetByIdAsync(id);
        if (tag == null)
        {
            return NotFound();
        }
        return Ok(tag);
    }

    /// <summary>
    /// 根据名称获取标签
    /// </summary>
    [HttpGet("name/{name}")]
    public async Task<ActionResult<Tag>> GetByName(string name)
    {
        var tag = await _tagService.GetByNameAsync(name);
        if (tag == null)
        {
            return NotFound();
        }
        return Ok(tag);
    }

    /// <summary>
    /// 创建标签
    /// </summary>
    [HttpPost]
    public async Task<ActionResult<Tag>> Create(Tag tag)
    {
        var createdTag = await _tagService.CreateAsync(tag);
        return CreatedAtAction(nameof(GetById), new { id = createdTag.TagId }, createdTag);
    }

    /// <summary>
    /// 更新标签
    /// </summary>
    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id, Tag tag)
    {
        if (id != tag.TagId)
        {
            return BadRequest();
        }

        await _tagService.UpdateAsync(tag);
        return NoContent();
    }

    /// <summary>
    /// 删除标签
    /// </summary>
    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        await _tagService.DeleteAsync(id);
        return NoContent();
    }
} 